//
//  HZPeiziMoneyFlowView.m
//  Stock_Stategy
//
//  Created by MayerF on 2017/12/13.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZPeiziMoneyFlowView.h"
#import "HZPopMenuView.h"
#import "MYUpDownBtn.h"
#import "HZPeiziHeaderView.h"
#import "HZPeiziMoneyFlowCell.h"

@interface HZPeiziMoneyFlowView()<UITableViewDataSource, UITableViewDelegate, HZPopMenuViewDelegate>
@property (nonatomic, strong)HZPopMenuView    *pPopMenuView;
@property (nonatomic, strong)HZPeiziHeaderView *pHeaderView;
@property (nonatomic, strong)UITableView *pTableView;
@property (nonatomic, strong)NSArray *pDataList;
@property(nonatomic,strong)MYUpDownBtn    *pAlertInfo;
@end

@implementation HZPeiziMoneyFlowView

#pragma mark -
#pragma mark 生命周期
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = kMYHexToRGB(0xf6f6f6, 1);
        [self initCommon];
        [self initConstraints];
    }
    return self;
}
- (void)initCommon
{
    _pPopMenuView = [HZPopMenuView new];
    _pTableView = [UITableView new];
    _pTableView.dataSource = self;
    _pTableView.delegate = self;
    _pTableView.tableFooterView = [UIView new];
    
    _pTableView.bounces = NO;
    _pTableView.backgroundColor = kMYHexToRGB(0xf6f6f6, 1);
    _pPopMenuView.delegate = self;
    
    [self p_setPopMenu];
}
- (void)initConstraints
{
    [self addSubview:_pPopMenuView];
    [self addSubview:_pTableView];
    
    [_pPopMenuView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(self);
        make.height.mas_equalTo(45);
    }];
    [_pTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pPopMenuView.mas_bottom).offset(10);
        make.left.right.bottom.mas_equalTo(self);
    }];
}

- (void)updateAccountData:(NSArray<HZPeiziTradeFlowListModel *> *)data
{
    _pAlertInfo.hidden = YES;
    if (data && data.count > 0) {
        NSMutableArray *tempArray = [NSMutableArray array];
        for (HZPeiziTradeFlowListModel *model in data) {
            HZPeiziMoneyFlowModel *flowModel = [HZPeiziMoneyFlowModel new];
            flowModel.mode_type = kCell_Flow_Type;
            flowModel.mode_one = model.stk_name;
            flowModel.mode_two = model.match_volume;
            flowModel.mode_three = model.bstype;
            flowModel.mode_four = model.match_date;
            flowModel.mode_downOne = model.stkcode;
            flowModel.mode_downTwo = model.match_price;
            flowModel.mode_downThree = [NSString stringWithFormat:@"%0.2f", [model.occur_balance floatValue]];
            flowModel.mode_downFour = model.match_time;
            
            [tempArray addObject:flowModel];
        }
        _pDataList = tempArray;
    }else {
        _pDataList = @[];
        [self p_loadAlertInfo];
    }
    [_pTableView reloadData];
}

- (void)p_loadAlertInfo{
    if (!_pAlertInfo) {
        _pAlertInfo = [MYUpDownBtn new];
        [_pAlertInfo setImage:[UIImage imageNamed:@"empty"] forState:UIControlStateNormal];
        [_pAlertInfo setImage:[UIImage imageNamed:@"empty"] forState:UIControlStateHighlighted];
        _pAlertInfo.imageView.contentMode = UIViewContentModeCenter;
        [_pAlertInfo setTitle:@"暂无数据" forState:UIControlStateNormal];
        [_pAlertInfo setTitleColor:kMYHexToRGB(0x888888, 1) forState:UIControlStateNormal];
        _pAlertInfo.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_pAlertInfo];
        [_pAlertInfo mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(self);
            make.left.mas_equalTo(self);
            make.right.mas_equalTo(self);
            make.height.mas_equalTo(70);
        }];
    }
    _pAlertInfo.hidden = NO;
}

- (void)p_setPopMenu
{
    NSArray *pTitleArray = @[@"收/支",@"发生日期"];
    _pPopMenuView.titleArray = pTitleArray;
    //初始化控件内容
    NSMutableArray *pContentArray = [NSMutableArray arrayWithCapacity:2];
    NSMutableArray *pTempDate = [NSMutableArray array];
    NSMutableArray *pMoneyType = [NSMutableArray array];
    
    NSArray *moneyArray = [NSArray arrayWithObjects:
                           @"全部",
                           @"收入",
                           @"支出",
                           nil];
    NSArray *dateArray = [NSArray arrayWithObjects:
                          @"全部",
                          @"7天之内",
                          @"15天之内",
                          nil];
    
    for (NSString *title in dateArray) {
        HZPopMenuModel *model = [HZPopMenuModel new];
        model.model_name = title;
        [pTempDate addObject:model];
        if ([title isEqualToString:@"7天之内"]) {
            model.isShowImage = YES;
        }
    }
    for (NSString *title in moneyArray) {
        HZPopMenuModel *model = [HZPopMenuModel new];
        model.model_name = title;
        [pMoneyType addObject:model];
        if ([title isEqualToString:@"全部"]) {
            model.isShowImage = YES;
        }
    }
    
    [pContentArray addObject:pMoneyType];
    [pContentArray addObject:pTempDate];
    _pPopMenuView.contentArray = pContentArray;
    [_pPopMenuView updateTitle:@"7天之内" index:1];
}
#pragma mark -
#pragma mark UITableViewDataSource
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _pDataList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UITableViewCell *cell = nil;
    HZPeiziMoneyFlowModel *model = _pDataList[indexPath.row];
    cell = [HZPeiziMoneyFlowCell cellTableView:tableView indexPath:indexPath cellModel:model];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (_pDataList.count > 0) {
        return self.pHeaderView;
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (_pDataList.count > 0) {
        return 30;
    }
    return 0;
}
#pragma mark -
#pragma mark HZPopMenuViewDelegate
- (void)popMenuViewDidSelectedRowWithContent:(NSArray *)content{
    LogicRouterBlock block = [self.logicManager matchLogicBlock:kLogicRouterPeiziFlowFind];
    if (block) {
        block(nil,content);
    }
}

- (HZPeiziHeaderView *)pHeaderView {
    if (!_pHeaderView) {
        _pHeaderView = [HZPeiziHeaderView new];
        [_pHeaderView setTexts:@[@"证券/代码",@"数量/价格",@"业务/金额",@"日期/时间"]];
    }
    return _pHeaderView;
}

@end
